<template>
	<el-form :model="formData" ref="formRef" :rules="formRules" label-width="100px" label-position="left" class="demo-ruleForm" style="overflow: hidden;">
        <el-form-item label="名　　称：" prop="name">
            <el-input type="text" v-model.trim="formData.name" @keyup.enter.native="enterKey($event, 'DPT1')" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="开 户 行：" prop="bankName">
            <el-input type="text" v-model.trim="formData.bankName" ref="DPT1" @keyup.enter.native="enterKey($event, 'DPT2')" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="账　　号：" prop="accountNumber">
            <el-input type="text" v-model.trim="formData.accountNumber" ref="DPT2" @keyup.enter.native="enterKey($event, 'DPT3')" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备　　注：" prop="remarks">
            <el-input type="text" v-model.trim="formData.remarks" autocomplete="off" ref="DPT3" @keyup.enter.native="save('formRef')"></el-input>
        </el-form-item>
        <el-form-item label="连续新增：" style="margin-bottom: 10px !important;" v-if="!rowId">
            <el-checkbox v-model="continueAdd"></el-checkbox>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" size="small" @click="save('formRef')">保存</el-button>
            <el-button type="danger" size="small" @click="close">关闭</el-button>
        </el-form-item>
    </el-form>
</template>
<script>
export default {
	data() {
		return {
			continueAdd: false,
			formData: {},
            formRules: {
                name: [{required: true, message: '名称不能为空', triggle: 'blur'}],
                bankName: [{required: true, message: '开户行不能为空', triggle: 'blur'}],
                accountNumber: [{required: true, message: '账号不能为空', triggle: 'blur'}]
            },
            rowId: this.$store.state.auxiliaryStore.rowId
		}
	},
	methods: {
        getUpdateData() {           //获取更新数据
            this.$http.get('auxiliaryInfoAllCategory/selectByBankId?id=' + this.rowId).then(res => {
                if(res.data.code == 20001) {
                    if(res.data.data) {
                        this.formData = res.data.data;
                    }
                }
            });
        },
        save(formName) {            //保存
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    let row = this.$store.state.auxiliaryStore.row[0];
                    if(!this.formData['id']) {          //新增保存
                        this.formData['infoId'] = row['id'];           //小类id
                        this.$http({
                            url: 'auxiliaryInfoAllCategory/insertBank',
                            method: 'post',
                            params: this.formData
                        }).then(res => {
                            if(res.data.code == 20001) {
                                this.$message({message: '新增成功', type: 'success', duration: 1500});
                                setTimeout(() => {
                                    this.$store.dispatch('auxiliaryStore/getSubClassTableData', row);
                                    this.continueAdd ? this.$refs[formName].resetFields() : this.close();
                                }, 1000);
                            } else {
                                this.$message({message: res.data.msg || '新增失败', type: 'error', duration: 1500});
                            }
                        });
                    } else {
                        //修改保存
                        delete this.formData['createTime'];
                        delete this.formData['updateTime'];
                        this.$http({
                            url: 'auxiliaryInfoAllCategory/updateBank',
                            method: 'post',
                            params: this.formData
                        }).then(res => {
                            if(res.data.code == 20001) {
                                this.$message({message: '修改成功', type: 'success', duration: 1500});
                                setTimeout(() => {
                                    this.$store.dispatch('auxiliaryStore/getSubClassTableData', row);
                                    this.close();
                                }, 1000);
                            } else {
                                this.$message({message: res.data.msg || '修改失败', type: 'error', duration: 1500});
                            }
                        });
                    }
                }
            });
        },
        close() {           //关闭当前弹窗
            let index = parent.layer.getFrameIndex(window.name);    //先得到当前iframe层的索引
            parent.layer.close(index);
        },
        enterKey(lastRef, nextRef) {		//回车键换行
            if(lastRef.target.value) {
                this.$refs[nextRef].focus();
            }
        }
    },
    created() {
        if(this.rowId) this.getUpdateData();
    }
}
</script>